﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CurriculumnManage.DBAccess;
using CurriculumnManage.Helper;

namespace CurriculumnManage
{
    public partial class frmMailAddress : Form
    {
        public string iSubjectGradeProfileID;
        private frmMail frmMail;
        public frmMailAddress(frmMail mail)
        {
            InitializeComponent();
            this.frmMail = mail;
        }

        private void frmMailAddress_Load(object sender, EventArgs e)
        {
            try
            {
                ShowTeacher();
            }
            catch (Exception ex)
            {
                CommonFunction.ShowError("frmMailAddress.frmMailAddress_Load", ex.Message);
            }
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                int iSelectedGridRow = -1;
                string strTeacherMail = "";
                string strTeacherName = "";
                if (this.gvTeacherMail.SelectedRows.Count < 1)
                    return;

                //if (this.gvTeacherMail.RowCount < 2 || this.gvTeacherMail.SelectedRows[0].Index > this.gvTeacherMail.RowCount - 2)
                //    return;
                iSelectedGridRow = this.gvTeacherMail.SelectedRows[0].Index;

                if (this.gvTeacherMail["vMail", iSelectedGridRow].Value.ToString().Length > 0)
                    strTeacherMail = gvTeacherMail["vMail", iSelectedGridRow].Value.ToString();
                else
                {
                    MyLog.TraceInstance.TraceInformation("MailAddress.btnSend_Click:Not Exist strTeacherMail");
                    return;
                }
                if (this.gvTeacherMail["vSyudaisyaName", iSelectedGridRow].Value.ToString().Length > 0)
                    strTeacherName = this.gvTeacherMail["vSyudaisyaName", iSelectedGridRow].Value.ToString();
                else
                {
                    MyLog.TraceInstance.TraceInformation("MailAddress.btnSend_Click:Not Exist strTeacherName");
                    return;
                }
                frmMail.teacher = strTeacherName;
                frmMail.txtAddr.Text = strTeacherName + "<" + strTeacherMail + ">";
                frmMail.address = strTeacherMail;
                frmMail.txtBody.Text = frmMail.txtBody.Text.Replace("#Teacher", strTeacherName);
                this.Dispose();
            }
            catch (Exception ex)
            {
                CommonFunction.ShowError("frmMailAddress.btnSend_Click", ex.Message);
            }

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ShowTeacher()
        {
            try
            {
                if (string.IsNullOrEmpty(iSubjectGradeProfileID)) return;
                using (DBContext db = new DBContext())
                {
                    SqlResult result = db.CallProcedure("usp_TeacherMail_get", MyUtils.ParseToInt(iSubjectGradeProfileID, 0));
                    if (result.DataSet.Tables.Count > 0 && result.DataSet.Tables[0].Rows.Count > 0)
                    {
                        this.gvTeacherMail.DataSource = result.DataSet.Tables[0];
                    }
                }
                gvTeacherMail.ClearSelection();
                gvTeacherMail.AutoResizeRows();
            }
            catch (Exception ex)
            {
                CommonFunction.ShowError("frmMailAddress.ShowTeacher", ex.Message);
            }
        }
    }
}
